package com.richapp.launcher.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import com.richapp.launcher.busevent.EventServiceDestroy;
import com.richapp.launcher.net.box.tcp.server.BoxServer;

import de.greenrobot.event.EventBus;

/**
 * Created by jack on 2015/5/5 0005.
 */
public class BoxService extends Service{

    private BoxServer boxServer = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        boxServer = BoxServer.getInstance();
        boxServer.setServerName("机顶盒<-机顶盒服务");
        boxServer.setContext(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                boxServer.start();
            }
        }).start();
        //保持服务
        Notification notification = new Notification();
        startForeground(3, notification);
        //Toast.makeText(this, "BOX服务启动", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        boxServer.stop();
        EventBus.getDefault().post(new EventServiceDestroy(BoxService.class));
    }
}
